﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ZLDC.Common.Estate;
namespace ZLDC.Estate.Quantify.Report
{
    public partial class lp_today_list : System.Web.UI.Page
    {
        public StringBuilder sb = new StringBuilder();
      
        public string depttype = "";//部门类型
        public string deptid="0";
        public string statrday = "";
        public string endday = "";
        public string type = "";
        public int total;
        public int Actual_Goal;
        public string jsdeptid = "";
        public string todaydepttype = "";
        public string todaydeptid = "0";//昨日工作使用
        protected void Page_Load(object sender, EventArgs e)
        {
            deptid = _Request.GetString("deptid", _Utility.GetInfoBySession().m_sDeptID);
            jsdeptid = _Request.GetString("deptid", _Utility.GetInfoBySession().m_sDeptID);
            statrday = _Request.GetString("statrday", "0");
            endday = _Request.GetString("endday", "0");
            type = _Request.GetString("type", "0");
            todaydepttype = ZLDC.Topo.BLL.Estate.lp_Report_list.GetType(_Utility.GetInfoBySession().m_sDeptID);//昨日工作使用
            todaydeptid = _Utility.GetInfoBySession().m_sDeptID;
            getOnload();

        }

        protected void getOnload()
        {
            if (!IsPostBack)
            {
                string str = _Utility.GetInfoBySession().m_sStation;
                //string ssss = _Utility.GetInfoBySession().m_sRoleName;
                if (ZLDC.Topo.BLL.Estate.lp_Basis.GetStationbq(str))
                {
                    GetTableCount(_Utility.GetInfoBySession().m_sDeptID);

                    if (deptid == "0")
                    {
                        jsdeptid = _Utility.GetInfoBySession().m_sDeptID;
                        depttype = todaydepttype;//获取部门类型
                        GetTB(_Utility.GetInfoBySession().m_sDeptID, "'" + DateTime.Now.ToString("yyyy-MM-dd") + "'", "'" + DateTime.Now.ToString("yyyy-MM-dd") + "'");
                    }
                    else
                    {
                        depttype = ZLDC.Topo.BLL.Estate.lp_Report_list.GetType(deptid);//获取部门类型
                        GetTB(deptid, "'" + DateTime.Now.ToString("yyyy-MM-dd") + "'", "'" + DateTime.Now.ToString("yyyy-MM-dd") + "'");
                    }

                }
                else
                {
                    Response.Redirect("lp_Report_Userlist.aspx?type=1&userid=" + _Utility.GetInfoBySession().m_sUserID + "&statrday=" + DateTime.Now.ToString("yyyy-MM-dd") + "&endday=" + DateTime.Now.ToString("yyyy-MM-dd") + "");
                }
            }
            GetAnone();
        }


        /// <summary>
        /// 是否显示返回
        /// </summary>
        protected void GetAnone()
        {
            if (type != "0")
            {
                anone.Style["display"] = "";
            }
        }
        /// <summary>
        /// 查询点击分行 是否有权限
        /// </summary>
        protected void GetTableCount(string deptid)
        {
            if (int.Parse(ZLDC.Topo.BLL.Estate.lp_Report_list.GetTableCount(deptid)) > 0)
            {
                ViewState["tbcount"] = "1";
            }

        }

        public void Gett()
        {
            ZLDC.Topo.BLL.Estate.lp_YesterDay.GetNum(DateTime.Now.AddDays(-1), Average, YesterDay, CacheUtility.Cache_GetAllChildDept(_Utility.GetInfoBySession().m_sDeptID), District, _Utility.GetInfoBySession().m_sDeptID,ZLDC.Topo.BLL.Estate.lp_Report_list.GetType(_Utility.GetInfoBySession().m_sDeptID),_Utility.GetInfoBySession().m_sUserID);

        }

        /// <summary>
        /// 同级统计昨日的平均分，排名
        /// </summary>
        public void deptnum(string depttype)
        {
            divdeptnum.Style["display"] = "";
            if (deptid == "0")
            {
                if (depttype == "分行")
                    ZLDC.Topo.BLL.Estate.lp_YesterDay.GetNum(DateTime.Now.AddDays(-1), Average, YesterDay, District, _Utility.GetInfoBySession().m_sDeptID, depttype, _Utility.GetInfoBySession().m_sDeptID,ZLDC.Topo.BLL.Estate.lp_Basis.getdepttjlist(_Utility.GetInfoBySession().m_sDeptID));
                else
                    ZLDC.Topo.BLL.Estate.lp_YesterDay.GetNum(DateTime.Now.AddDays(-1), Average, YesterDay, District, ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptxjlist(_Utility.GetInfoBySession().m_sDeptID), depttype, _Utility.GetInfoBySession().m_sDeptID, ZLDC.Topo.BLL.Estate.lp_Basis.getdepttjlist(_Utility.GetInfoBySession().m_sDeptID));

            }
            else
            {
                if(depttype=="区域")
                    ZLDC.Topo.BLL.Estate.lp_YesterDay.GetNum(DateTime.Now.AddDays(-1), Average, YesterDay, District, ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptxjlist(deptid), depttype, deptid,  ZLDC.Topo.BLL.Estate.lp_Basis.getdepttjlist(deptid));
                else
                    ZLDC.Topo.BLL.Estate.lp_YesterDay.GetNum(DateTime.Now.AddDays(-1), Average, YesterDay, District, ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptxjlist(deptid), depttype, deptid, ZLDC.Topo.BLL.Estate.lp_Basis.getdepttjlist(deptid));
            }
        }

        #region 分行 区经 营业部
        protected void GetTB(string deptId, string statrday, string endday)
        {
             total = 0;//总要求得分
             Actual_Goal = 0;//实际总得分
            string sqlfile = "select SUM(total) as total,sum(Actual_Goal) as Actual_Goal  ";
            string sql = "   where 1=1 ";//经理分配与自己选择
            string group = "group by  ";
            string sqlwhere = " where 1=1 "; //饼图显示条件
            bool Prompt = true;//判断是否有数据，显示未分配
            
            if (depttype == "营业大部")
            {
                sb.Append("今日工作概况<table id=\"tb1\" class=\"table\" border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
                sb.Append("<thead>");
                sb.Append("<tr>");
                sqlfile += ",Other6,Other7 ";
                sql += " and Other8=" + deptId + "";
                sqlwhere += "and Other8='" + deptId + "'";
                group += ",Other6,Other7 order by total desc";
                
                sb.Append("<th align=\"center\" width=\"90\"  bgcolor='d3eaef' class='STYLE6'>");
                sb.Append("营业部");
                sb.Append("</th>");
            }
            else if (depttype == "营业部")
            {
                sb.Append("今日工作概况<table id=\"tb1\" class=\"table\" border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
                sb.Append("<thead>");
                sb.Append("<tr>");
                sqlfile += ",Other4,Other5 ";
                sql += " and Other6=" + deptId + "";
                sqlwhere += "and Other6='" + deptId + "'";
                group += "Other4,Other5 order by total desc";
                sb.Append("<th align=\"center\" width=\"90\"  bgcolor='d3eaef' class='STYLE6'>");
                sb.Append("区域");
                sb.Append("</th>");
                deptnum(depttype);
            }
            else if (depttype == "区域")
            {
                sb.Append("今日工作概况<table id=\"tb1\" class=\"table\" border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
                sb.Append("<thead>");
                sb.Append("<tr>");
                sqlfile += ",deptid,Other3 ";
                sql += " and Other4=" + deptId + "";
                sqlwhere += "and Other4='" + deptId + "'";
                group += "deptid,Other3 order by total desc";
                sb.Append("<th align=\"center\" width=\"90\"  bgcolor='d3eaef' class='STYLE6'>");
                sb.Append("分行");
                sb.Append("</th>");
                deptnum(depttype);
            }
            else if (depttype == "分行")
            {
                sb.Append("今日工作概况<table id=\"tb1\" class=\"table\" border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
                sb.Append("<thead>");
                sb.Append("<tr>");
                if (ViewState["tbcount"] != null && ViewState["tbcount"].ToString() == "1")
                {
                    sqlfile += ",Other1,leave,userid,Other2 ";

                    sql += " and deptid=" + deptId + "";
                    sqlwhere += "and Other2='" + deptId + "'";
                    group += "userid,Other2,Other1,leave order by total desc";
                    sb.Append("<th align=\"center\" bgcolor='d3eaef' class='STYLE6'>");
                    sb.Append("姓名");
                    sb.Append("</th>");
                    deptnum(depttype);
                    btnUpdate.Visible = true;
                }
                else
                {
                    progressbar1.Style["display"] = "none";
                    return;
                }
            }
            else
            {
                progressbar1.Style["display"] = "none";
                return;
            }
            sqlfile += " from lp_Configuration_main";
            sb.Append("<th align=\"center\" bgcolor='d3eaef' class='STYLE6'>");
            sb.Append("实际得分");
            sb.Append("</th>");
            sb.Append("<th align=\"center\" bgcolor='d3eaef'  class='STYLE6'>");
            sb.Append("要求得分");
            sb.Append("</th>");
            sb.Append("<th align=\"center\" bgcolor='d3eaef'  class='STYLE6'>");
            sb.Append("完成率%");
            sb.Append("</th>");
            sb.Append("</tr>");
            sb.Append("</thead>");
            
           // sql += " and workday ='" + DateTime.Now.ToString("yyyy-MM-dd") + "' ";
            //string useridlist = ZLDC.Topo.BLL.Estate.lp_today_list.getUserList("select userID from lp_Configuration_main " + sql);//查询量化进来的所有用户
            
            sql = sqlfile + sql;

            //a_Average.InnerHtml = ZLDC.Topo.BLL.Estate.lp_today_list.geta_Average(sqlwhere); ;//显示各个级别的 平均分 和排名。 当天的
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql + group);
            
            if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds) )
            {
                Prompt = false;
               for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sb.Append("<tr>");
                    if (depttype == "营业大部")
                    {
                        sb.Append("<td style=\"background-color:White;text-align:center\">");
                        sb.Append("<a href=\"lp_today_list.aspx?type=3&deptid=" + ds.Tables[0].Rows[i]["Other6"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["Other7"].ToString() + "</a>");
                        sb.Append("</td>");
                    }
                    if (depttype == "营业部")
                    {
                        sb.Append("<td style=\"background-color:White;text-align:center\">");
                        sb.Append("<a href=\"lp_today_list.aspx?type=2&deptid=" + ds.Tables[0].Rows[i]["Other4"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["Other5"].ToString() + "</a>");
                        sb.Append("</td>");
                    }
                    if (depttype == "区域")
                    {
                        sb.Append("<td style=\"background-color:White;text-align:center;width:90px;\">");
                        if (ViewState["tbcount"] != null && ViewState["tbcount"].ToString() == "1")
                        {
                            sb.Append("<a href=\"lp_today_list.aspx?type=1&deptid=" + ds.Tables[0].Rows[i]["deptid"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["Other3"].ToString() + "</a>");
                        }
                        else
                        {
                            sb.Append("<a href=\"#\">" + ds.Tables[0].Rows[i]["Other3"].ToString() + "</a>");
                        }
                        sb.Append("</td>");
                    }
                    if (depttype == "分行")
                    {
                        sb.Append("<td  style=\"background-color:White;text-align:center\">");
                        if (ds.Tables[0].Rows[i]["leave"].ToString() == "1")
                        {
                            sb.Append("<a  onclick=\"FKalert(" + ds.Tables[0].Rows[i]["userID"].ToString() + "," + statrday + "," + endday + ",'" + ds.Tables[0].Rows[i]["Other2"].ToString() + "')\" >" + ds.Tables[0].Rows[i]["Other2"].ToString() + "<span style='color:red'>(请假)</span></a>");
                        }
                        else if (ds.Tables[0].Rows[i]["Other1"].ToString() == "")
                        {
                            sb.Append("<a  onclick=\"FKalert(" + ds.Tables[0].Rows[i]["userID"].ToString() + "," + statrday + "," + endday + ",'" + ds.Tables[0].Rows[i]["Other2"].ToString() + "')\" >" + ds.Tables[0].Rows[i]["Other2"].ToString() + "<span style='color:red'>(未分配)</span></a>");
                        }
                        else
                        {
                            sb.Append("<a  onclick=\"FKalert(" + ds.Tables[0].Rows[i]["userID"].ToString() + "," + statrday + "," + endday + ",'" + ds.Tables[0].Rows[i]["Other2"].ToString() + "')\">" + ds.Tables[0].Rows[i]["Other2"].ToString() + "</a>");
                        }
                            sb.Append("</td>");
                    }
                    sb.Append("<td style=\"background-color:White;text-align:center\">");
                        sb.Append(ds.Tables[0].Rows[i]["Actual_Goal"].ToString());
                        Actual_Goal += int.Parse(ds.Tables[0].Rows[i]["Actual_Goal"].ToString());
                    sb.Append("</td>");
                    sb.Append("<td style=\"background-color:White;text-align:center\">");
                    sb.Append(ds.Tables[0].Rows[i]["total"].ToString());
                    total += int.Parse(ds.Tables[0].Rows[i]["total"].ToString());
                    sb.Append("</td>");

                    sb.Append("<td style=\"background-color:White;text-align:center\">");
                    if (ds.Tables[0].Rows[i]["total"].ToString() == "0")
                    {
                        sb.Append("0");
                    }
                    else
                    {
                        sb.Append(int.Parse(ds.Tables[0].Rows[i]["Actual_Goal"].ToString())*100 / int.Parse(ds.Tables[0].Rows[i]["total"].ToString()));
                    }
                    sb.Append("</td>");
                }
            }
            sb.Append("</tr>");
            sb.Append("<tr>");
            sb.Append("<td  style=\"background-color:White;text-align:center\">");
            sb.Append("总分:");
            sb.Append("</td>");
            sb.Append("<td  style=\"background-color:White;text-align:center\">");
            sb.Append(Actual_Goal.ToString());
            sb.Append("</td>");
            sb.Append("<td  style=\"background-color:White;text-align:center\">");
            sb.Append(total.ToString());
            sb.Append("</td>");
            sb.Append("<td  style=\"background-color:White;text-align:center\">");
            if (total == 0)
            {
                sb.Append("0");
            }
            else
            {
                sb.Append((Actual_Goal*100) / total);
            }
            sb.Append("</td>");
            sb.Append("</tr>");
           
            sb.Append("</table>");
            if (Prompt)//是否没数据 无，则显示未安排工作
            {
                sb.Append("<span style='color:red'>经理未安排工作！</span>");
            }
            GetCharts(myNext, ZLDC.Topo.BLL.Estate.lp_Report_list.GetSql_1(sqlwhere, statrday, endday), depttype);
        }

        #endregion
        /// <summary>
        /// 显示饼图
        /// </summary>
        /// <param name="mylist"></param>
        /// <param name="mynext"></param>
        public void GetCharts(Literal mynext, string sql,string type)
        {
            StringBuilder sb1 = new StringBuilder();
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ds1.Tables.Count > 0)
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    sb1.Append("{name:'" + ds1.Tables[0].Rows[i]["BasisName"].ToString() + ds1.Tables[0].Rows[i]["Actual_Number"].ToString() + "',");
                    sb1.Append(" y:" + ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() + ", ");
                    sb1.Append("dataLabels:'" + ZLDC.Topo.BLL.Estate.lp_Basis.Get_ChartUrl(ds1.Tables[0].Rows[i]["BasisName"].ToString()) + "&deptid=" + jsdeptid + "&addDate2=" + DateTime.Now.ToString("yyyy-MM-dd") + "&addDate1=" + DateTime.Now.ToString("yyyy-MM-dd") + "'},");
                    
                }
            }
            hidchart.Value = sb1.ToString();
          

        }

       

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ZLDC.DAL.Estate.lp_Basis.lp_pro_updatelist("2","", _Utility.GetInfoBySession().m_sDeptID, "");
            Response.Write("<script>window.location.href=location.href;</script>");
        }

    }
}
